package com.appstore.service.impl;

import com.appstore.mapper.AppCommentMapper;
import com.appstore.mapper.AppImageMapper;
import com.appstore.mapper.AppMapper;
import com.appstore.domain.entity.App;
import com.appstore.domain.entity.AppComment;
import com.appstore.domain.entity.AppImage;
import com.appstore.domain.vo.AppDetailVO;
import com.appstore.service.AppService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AppServiceImpl implements AppService {

    @Resource
    private AppMapper appMapper;
    @Resource
    private AppCommentMapper appCommentMapper;
    @Resource
    private AppImageMapper appImageMapper;
    @Override
    public List<App> getAppList() {
        return appMapper.getAppList();
    }

    @Override
    public List<App> getAppByConditions(App app) {
        return appMapper.getAppByConditions(app);
    }

    @Override
    public AppDetailVO getAppDetail(Long id) {
        AppDetailVO appDetailVO = new AppDetailVO();
        List<AppComment> appCommentList = appCommentMapper.getAppCommentListByAppId(id);
        List<AppImage> appImageList = appImageMapper.getByAppId(id);
        App app = appMapper.getAppById(id);
        BeanUtils.copyProperties(app, appDetailVO);
        appDetailVO.setAppComments(appCommentList);
        appDetailVO.setAppImages(appImageList);
        return appDetailVO;
    }

    @Override
    public App getAppById(Long appId) {
        return appMapper.getAppById(appId);
    }

    @Override
    public Integer addApp(App app) {
        return appMapper.addApp(app);
    }

    @Override
    public App getAppByBundleName(String originalFilename) {
        return appMapper.getAppByBundleName(originalFilename);
    }

    @Override
    public List<App> getEssentialForNewDevices() {
        return appMapper.getEssentialForNewDevices();
    }

    @Override
    public int updateById(App app) {
        return appMapper.updateApp(app);
    }
}
